package com.atsmart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.ClientInfo;
import com.atsmart.app.model.ClientListInfo;

import com.atsmart.app.widgets.PlaceHolderImageview;

public class ClientSelectListAdapter extends LoadingAdapter {
	private ClientListInfo datasource;
	private LayoutInflater inflater;

	public static interface ClickDelegate{
		void onItemClick(int position);
	}

	protected ClickDelegate clickDelegate;

	public ClickDelegate getClickDelegate(){
		return clickDelegate;
	}

	public void setClickDelegate(ClickDelegate delegate){
		this.clickDelegate = delegate;
	}

	public ClientSelectListAdapter(ClientListInfo datasource, Context context) {
		this.datasource = datasource;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void appendDataSource(ClientListInfo appendData){
		datasource.getClients().addAll(appendData.getClients());
	}

	private static class ViewHolder{
		PlaceHolderImageview icon;
		TextView name;
		TextView address;
		TextView tel;
		TextView num;
	}

	@Override
	public int getCount() {
		if (datasource.getClients() == null || datasource.getClients().size() == 0) {
            return 0;
        }

        if (delegate != null && delegate.needLoading()) {
            return datasource.getClients().size() + 1;
        } else {
            return datasource.getClients().size();
        }
	}

	@Override
	public Object getItem(int position) {
		 if (datasource.getClients() == null || position == datasource.getClients().size()
	                && delegate != null && delegate.needLoading()) {
	            return null;
	        }
		return datasource.getClients().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
        if (loadingRow != null) {
            return loadingRow;
        }
        return getView(position, convertView);
	}

	private View getView(final int position, View convertView) {
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			convertView = inflater.inflate(R.layout.client_list_item_layout, null);
			vh = new ViewHolder();
			vh.icon = (PlaceHolderImageview) convertView.findViewById(R.id.img_client_icon);
			vh.name = (TextView) convertView.findViewById(R.id.txt_client_name);
			vh.address = (TextView) convertView.findViewById(R.id.txt_client_address);
			vh.tel = (TextView) convertView.findViewById(R.id.txt_client_tel);
			vh.num = (TextView) convertView.findViewById(R.id.txt_client_tmp_num);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		ClientInfo clientInfo = datasource.getClients().get(position);
		vh.icon.setImageUrl(clientInfo.getImgUrl());
		if (BizApp.getInstance().getCurrentLocation() == null || clientInfo.getDistance() == 0) {
			vh.name.setText(clientInfo.getName());
		} else if (clientInfo.getDistance() > 0) {
			if (clientInfo.getDistance() < 1000) {
				vh.name.setText(clientInfo.getName() + "[" + String.format("%.1f", clientInfo.getDistance()) + "米]");
			} else {
				vh.name.setText(clientInfo.getName() + "[" + String.format("%.1f", clientInfo.getDistance() / 1000) + "公里]");
			}
		}

		vh.address.setText(clientInfo.getAddress());
		vh.tel.setText(clientInfo.getTel());
//		if (clientInfo.getTmpNum() == 0) {
//			vh.num.setVisibility(View.INVISIBLE);
//		} else {
//			vh.num.setVisibility(View.VISIBLE);
//			vh.num.setText(String.valueOf(clientInfo.getTmpNum()));
//		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clickDelegate.onItemClick(position);
			}
		});
		return convertView;
	}
	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getClients().size();
	}

}
